require "scripts.少年光头.Helper.getMouse"
require "scripts.少年光头.Helper.deepcopy"
require "scripts.少年光头.Helper.IteamTxt"
require "scripts.少年光头.1a.GlobalData"
UiHelper.itemtip = obj:extend()

---物品提示
---type "物品栏" "鼠标"
function UiHelper.itemtip:new(type)
    local img = backgroundData.transparentImg
    --自适应背景
    self.bg = UiHelper.backdrop({fdfName = "sysItemTip"}):dfClearAllPoints()

    self.type = type
    --属性显示
    if type == "鼠标" then
        self.frameLB =
            UiHelper.label({fdfName = "txt"}):dfsize(0, 0):dfpointp(
            0.1,
            0,
            hdzui.origin.game(),
            FRAME_ALIGN_CENTER,
            FRAME_ALIGN_CENTER
        )
    end

    if type == "物品栏" then
        self.frameLB =
            UiHelper.label():dfsize(0, 0):dfpointp(
            -0.002,
            0.06,
            hdzui.origin.itemSlot()[1],
            FRAME_ALIGN_LEFT_BOTTOM,
            FRAME_ALIGN_LEFT_TOP
        ):dftext("物品栏物品提示")
    end

    -- self.MoneyBG =
    --     UiHelper.backdrop({parent = self.bg.frameId}):dfClearAllPoints():dfsize(0, 0.01):dfbackdrop(img):dfpoint(
    --     0,
    --     0,
    --     FRAME_ALIGN_LEFT_BOTTOM,
    --     self.frameLB.frameId,
    --     FRAME_ALIGN_LEFT_TOP
    -- ):dfpoint(0, 0, FRAME_ALIGN_RIGHT_BOTTOM, self.frameLB.frameId, FRAME_ALIGN_RIGHT_TOP)

    -- self.NameBG =
    --     UiHelper.backdrop({parent = self.bg.frameId}):dfClearAllPoints():dfsize(0, 0.01):dfbackdrop(img):dfpoint(
    --     0,
    --     0,
    --     FRAME_ALIGN_LEFT_BOTTOM,
    --     self.MoneyBG.frameId,
    --     FRAME_ALIGN_LEFT_TOP
    -- ):dfpoint(0, 0, FRAME_ALIGN_RIGHT_BOTTOM, self.MoneyBG.frameId, FRAME_ALIGN_RIGHT_TOP)

    -- self.goldbg =
    --     UiHelper.backdrop({parent = self.bg.frameId}):dfClearAllPoints():dfsize(0.01, 0.01):dfbackdrop(
    --     backgroundData.goldImg
    -- ):dfpoint(0, 0, FRAME_ALIGN_LEFT_TOP, self.MoneyBG.frameId, FRAME_ALIGN_LEFT_TOP):dfpoint(
    --     0,
    --     0,
    --     FRAME_ALIGN_LEFT_BOTTOM,
    --     self.MoneyBG.frameId,
    --     FRAME_ALIGN_LEFT_BOTTOM
    -- )

    -- self.goldlb =
    --     UiHelper.label({parent = self.bg.frameId}):dfClearAllPoints():dfsize(0.015, 0.01):dfpoint(
    --     0,
    --     0,
    --     FRAME_ALIGN_LEFT_TOP,
    --     self.goldbg.frameId,
    --     FRAME_ALIGN_RIGHT_TOP
    -- ):dfpoint(0, 0, FRAME_ALIGN_LEFT_BOTTOM, self.goldbg.frameId, FRAME_ALIGN_RIGHT_BOTTOM):dftext("0")

    -- self.lumberbg =
    --     UiHelper.backdrop({parent = self.bg.frameId}):dfClearAllPoints():dfsize(0.01, 0.01):dfbackdrop(
    --     backgroundData.lumberImg
    -- ):dfpoint(0, 0, FRAME_ALIGN_LEFT_TOP, self.goldlb.frameId, FRAME_ALIGN_RIGHT_TOP):dfpoint(
    --     0,
    --     0,
    --     FRAME_ALIGN_LEFT_BOTTOM,
    --     self.goldlb.frameId,
    --     FRAME_ALIGN_RIGHT_BOTTOM
    -- )

    -- self.lumberlb =
    --     UiHelper.label({parent = self.bg.frameId}):dfClearAllPoints():dfsize(0.015, 0.01):dfpoint(
    --     0,
    --     0,
    --     FRAME_ALIGN_LEFT_TOP,
    --     self.lumberbg.frameId,
    --     FRAME_ALIGN_RIGHT_TOP
    -- ):dfpoint(0, 0, FRAME_ALIGN_LEFT_BOTTOM, self.lumberbg.frameId, FRAME_ALIGN_RIGHT_BOTTOM):dftext("0")

    -- self.itemNamelb =
    --     UiHelper.label({parent = self.bg.frameId}):dfClearAllPoints():dfsize(0, 0.01):dfpoint(
    --     0,
    --     0,
    --     FRAME_ALIGN_LEFT_TOP,
    --     self.NameBG.frameId,
    --     FRAME_ALIGN_LEFT_TOP
    -- ):dfpoint(0, 0, FRAME_ALIGN_LEFT_BOTTOM, self.NameBG.frameId, FRAME_ALIGN_LEFT_BOTTOM):dftext("名字")
    --local fam = {family = "fonts.ttf", size = 8 * 0.001, flag = 0, align = TEXT_ALIGN_LEFT}

    self.bg:dfpoint(-0.005, 0.005, FRAME_ALIGN_LEFT_TOP, self.frameLB.frameId, FRAME_ALIGN_LEFT_TOP):dfpoint(
        0.005,
        -0.005,
        FRAME_ALIGN_RIGHT_BOTTOM,
        self.frameLB.frameId,
        FRAME_ALIGN_RIGHT_BOTTOM
    )

    self:hide()

    return self
end

function UiHelper.itemtip:show()
    if self.type == "物品栏" then
        hdzui.framePoint(hdzui.origin.tooltip(), hdzui.origin.game(), FRAME_ALIGN_LEFT_TOP, FRAME_ALIGN_LEFT_TOP, 1, 1)
    end
    self.frameLB:dfshow()
    self.bg:dfshow()
end
function UiHelper.itemtip:hide()
    self.frameLB:dfhide()
    self.bg:dfhide()
    if self.type == "物品栏" then
        hdzui.framePoint(
            hdzui.origin.tooltip(),
            hdzui.origin.itemSlot()[1],
            FRAME_ALIGN_LEFT_BOTTOM,
            FRAME_ALIGN_LEFT_TOP,
            -0.002,
            0.06
        )
    end
end

---物品栏提示
function UiHelper.itemtip:InventoryItemTipOpen(diyItems)
    for i = 1, 6 do
        hdzui.onMouse(
            hdzui.origin.itemSlot()[i],
            MOUSE_ORDER_ENTER,
            function()
                local unit = hplayer.getSelection(hplayer.loc())
                hitem.forEach(
                    unit,
                    function(enumItem, slotIndex)
                        if slotIndex == (i - 1) then
                            if enumItem ~= nil then
                                self:setTexture(enumItem, diyItems)
                                self:show()
                            end
                        end
                    end
                )
            end
        )

        hdzui.onMouse(
            hdzui.origin.itemSlot()[i],
            MOUSE_ORDER_LEAVE,
            function()
                if hplayer.loc() == hplayer.loc() then
                    self:hide()
                 ---self.wupinglantime.destroy()
                end
            end
        )
    end
end

---鼠标物品提示
function UiHelper.itemtip:MouseItemTipOpen(diyItems)
    local c = 0
    htime.setInterval(
        0.03,
        function(curTimer)
            if hjapi.DzGetUnitUnderMouse() ~= nil then
                local enumItem = hjapi.DzGetUnitUnderMouse()

                local name = hitem.getName(enumItem)

                if name ~= "" and name ~= nil then
                    self:show()
                    local d = {
                        x = getMouseRX() + 0.07,
                        y = getMouseRY(),
                        point = FRAME_ALIGN_CENTER
                    }
                 
                    self:setTexture(enumItem, diyItems)
                    self.frameLB:dfAbsolutePoint(d.x, d.y, d.point)
                   --- print_mbr(hjapi.DzFrameGetHeight(self.frameLB.frameId))
                    
                else
                    self:hide()
                end
            else
                self:hide()
            end
        end
    )
end
---设置TIP文本
function UiHelper.itemtip:setTexture(itemid, diyItems)
    local txtdata = {}
    local txt = IteamTxt:Txt(itemid, diyItems)
    for index, value in ipairs(IteamTxt.itemtipkey) do
        if IteamTxt.itemtipkeyFalse[value] == nil then
            table.insert(txtdata, txt[value])
        end
        if value == "|n" then
            table.insert(txtdata, value)
        end
    end
    self.frameLB:dftext(table.concat(txtdata, "|n"))
    -- self.itemNamelb:dftext(txt["原物品名字"])
    -- if txt["黄金消耗"] ~= nil then
    --     self.goldlb:dftext(txt["黄金消耗"])
    --     self.goldlb:dfshow()
    --     self.goldbg:dfshow()
    -- else
    --     self.goldlb:dfhide()
    --     self.goldbg:dfhide()
    -- end

    -- if txt["木消耗"] ~= nil then
    --     self.lumberlb:dftext(txt["木消耗"])
    --     self.lumberlb:dfshow()
    --     self.lumberbg:dfshow()
    -- else
    --     self.lumberlb:dfhide()
    --     self.lumberbg:dfhide()
    -- end

    return self
end
